package com.zsh.wanandroid.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.blankj.utilcode.util.TimeUtils;
import com.zsh.wanandroid.R;
import com.zsh.wanandroid.bean.ArticleBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zsh
 * @date 2020/3/13.
 * 功能：fragment通用的adapter
 */

public class FragmentCommonAdapter extends RecyclerView.Adapter<FragmentCommonAdapter.ViewHolder> {

    private static long ONE_DAY = 1 * 24 * 60 * 60 * 1000; //1天

    private Context mContext;
    private LayoutInflater mInflater;
    private List<ArticleBean> mList = new ArrayList<>();

    private boolean isLike;

    public FragmentCommonAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setData(List<ArticleBean> list) {
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public void addData(List<ArticleBean> list) {
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public void clearData() {
        mList.clear();
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = mInflater.inflate(R.layout.content_item, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        //分享时间
        ArticleBean contentBean = mList.get(position);
        long shareDate = contentBean.getShareDate();
        //获取当前时间
        long nowMills = TimeUtils.getNowMills();
        holder.mTvMark2.setVisibility(View.GONE);
        if ((nowMills - shareDate) > ONE_DAY) {
            //分享时间大于1天
            holder.mTvMark1.setVisibility(View.GONE);
        } else {
            holder.mTvMark1.setVisibility(View.VISIBLE);
            holder.mTvMark1.setText("新");
        }
        holder.mTvShareUser.setText(contentBean.getShareUser());
        holder.mTvTime.setText(contentBean.getNiceDate());
        holder.mTvTitle.setText(contentBean.getTitle());
        holder.mTvChapterName.setText(contentBean.getSuperChapterName() + " / " + contentBean.getChapterName());
//        isLike = contentBean.isCollect();
//        holder.mCbLike.setChecked(isLike);
//        holder.mCbLike.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                likeClickListener.onItemClick(position, isLike);
//            }
//        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView mTvMark1, mTvMark2, mTvShareUser, mTvTime, mTvTitle, mTvChapterName;
        CheckBox mCbLike;

        public ViewHolder(View itemView) {
            super(itemView);
            mTvMark1 = (TextView) itemView.findViewById(R.id.tvMark1);
            mTvMark2 = (TextView) itemView.findViewById(R.id.tvMark2);
            mTvShareUser = (TextView) itemView.findViewById(R.id.tvShareUser);
            mTvTime = (TextView) itemView.findViewById(R.id.tvTime);
            mTvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
            mTvChapterName = (TextView) itemView.findViewById(R.id.tvChapterName);
            //mIvLike = (CheckBox) itemView.findViewById(R.id.ivLike);
        }
    }

    public interface LikeClickListener {
        void onItemClick(int position, boolean isLike);
    }

    //监听接口
    public LikeClickListener likeClickListener; //定义监听接口对象

    public void setLikeClickListener(LikeClickListener likeClickListener) {
        this.likeClickListener = likeClickListener;
    }
}
